package com.edu.d4_constance;

import javax.swing.*;
import javax.xml.crypto.Data;
import java.awt.event.ActionEvent;

/**
 * 常量做标记的作用,做信息标志和信息分类
 * 案例：超级玛丽
 */
public class ConstanceDemo2 {
    public static final int UP=1;//向上
    public static final int Down=2;//向下
    public static final int Left=3;//向左
    public static final int Right=4;//向右
    public static void main(String[] args) {
        //创建一个窗口格子
        JFrame frame = new JFrame();
        //创建一个桌布
        JPanel panel = new JPanel();
        //创建四个按钮对象
        JButton btn1 = new JButton("上");
        JButton btn2 = new JButton("下");
        JButton btn3 = new JButton("左");
        JButton btn4 = new JButton("右");
        //把按钮添加到桌布上
        panel.add(btn1);
        panel.add(btn2);
        panel.add(btn3);
        panel.add(btn4);
        //把桌布添加上窗口
        frame.add(panel);
        //设置窗口格式
        frame.setLocationRelativeTo(null);
        frame.setSize(400,300);
        frame.setVisible(true);
        //设置四个监听机制
        btn1.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                move(UP);
            }
        });
        btn2.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                move(Down);
            }
        });
        btn3.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                move(Left);
            }
        });
        btn4.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                move(Right);
            }
        });
    }
    public static void move(int a){
        switch(a){
            case UP:
                System.out.println("超级玛丽向上飞");
                break;
            case Down:
                System.out.println("超级玛丽向下飞");
                break;
            case Left:
                System.out.println("超级玛丽向左飞");
                break;
            case Right:
                System.out.println("超级玛丽向右飞");
                break;
            default:
                System.out.println("异常");
        }
    }
}
